package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.WxPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;


    /**
     * 调用微信支付获取 调起支付所需的所有参数，用于小程序端调起支付
     *
     * @param paymentType
     * @param orderNo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "调用微信支付获取 调起支付所需的所有参数，用于小程序端调起支付")
    @PostMapping("/wxPay/createJsapi/{paymentType}/{orderNo}")
    public Result<Map<String, String>> getWxPayParams(@PathVariable String paymentType, @PathVariable String orderNo) {
        Map<String, String> wxPayParams = wxPayService.getWxPayParams(paymentType, orderNo);
        return Result.ok(wxPayParams);
    }


    /**
     * 小程序端轮询查询交易支付状态
     * @param orderNo
     * @return true:已支付（小程序展示支付成功页面） false:未支付（继续轮询）
     */
    @Operation(summary = "小程序端轮询查询交易支付状态")
    @GetMapping("/wxPay/queryPayStatus/{orderNo}")
    public Result<Boolean> queryPayStatus(@PathVariable String orderNo){
        Boolean isPaid = wxPayService.queryPayStatus(orderNo);
        return Result.ok(isPaid);
    }


    /**
     * 处理微信支付成功后异步回调
     * @param request
     * @return
     */
    @Operation(summary = "处理微信支付成功后异步回调")
    @PostMapping("/wxpay/notify")
    public ResponseEntity notify(HttpServletRequest request){
        Map<String, String> map = wxPayService.notifySuccess(request);
        return ResponseEntity.ok(map);
    }


    //@Operation(summary = "处理微信支付成功后异步回调")
    //@PostMapping("/wxpay/notify1")
    //public Map<String, String> notify1(HttpServletRequest request){
    //    Map<String, String> map = new HashMap<>();
    //    map.put("code", "SUCCESS");
    //    map.put("message", "SUCCESS");
    //    return map;
    //}
}
